package vashion.azeroth.core.service.impl.item;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import vashion.azeroth.contant.CommonErrorEnum;
import vashion.azeroth.core.item.pojo.ItemDO;
import vashion.azeroth.core.item.query.ItemQueryDO;
import vashion.azeroth.core.manager.item.ItemReadManger;
import vashion.azeroth.core.personal.pojo.PersonalPublishFiles;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.PersonalPublishFilesService;
import vashion.azeroth.core.service.item.ItemReadService;
import vashion.azeroth.core.service.item.ItemRecommendService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhe on 15/11/23.
 */
@Service
public class ItemRecommendServiceImpl implements ItemRecommendService {

    private final static Logger log = LoggerFactory.getLogger(ItemRecommendServiceImpl.class);
    @Resource
    private ItemReadService itemReadService;
    @Resource
    private ItemReadManger itemReadManger;
    @Resource
    private PersonalPublishFilesService personalPublishFilesService;

    public ApiResult<List<ItemDO>> recommendItemByItemId(long itemId) {
        return ApiResult.newErrorResult(CommonErrorEnum.SYSTEM_ERROR.getErrorCode(), CommonErrorEnum.SYSTEM_ERROR.getErrorMessage());
    }

    public ApiResult<List<ItemDO>> recommendItemByUserId(long userId) {

        PersonalPublishFiles files = new PersonalPublishFiles();
        files.setMemberId(new Long(userId));
        files.setPageSize(100);
        ApiResult<Map<Integer, PersonalPublishFiles>> feedIdByTagResult = personalPublishFilesService.getFeedIdByTags(files, 0);
        Map<Long, Long> itemThreadMap = Maps.newHashMap();
        if (feedIdByTagResult.isSuccess()) {
            for (PersonalPublishFiles personalPublishFiles : feedIdByTagResult.getData().values()) {
                try {
                    Long itemId = personalPublishFiles.getItemIdFromTag();
                    if (null != itemId) {
                        itemThreadMap.put(itemId, personalPublishFiles.getPublishFileId().longValue());
                    }
                } catch (Exception e) {
                    log.error("personalPublishFiles.getItemIdFromTag error, personalPublishFiles=" + personalPublishFiles, e);
                }
            }
        }
        ItemQueryDO itemQueryDO = new ItemQueryDO();
        itemQueryDO.setItemIdList(Lists.newArrayList(itemThreadMap.keySet()));
        ApiResult<List<ItemDO>> itemListResult = itemReadManger.queryItem(itemQueryDO);
        //增加帖子Id
        for (ItemDO itemDO : itemListResult.getData()) {
            if (!itemThreadMap.containsKey(itemDO.getId())) {
                itemDO.setPublishFileId(itemThreadMap.get(itemDO.getId()));
            }
        }
        if (!itemListResult.isSuccess() || CollectionUtils.isEmpty(itemListResult.getData())) {
            log.error("此用户没有推荐商品！！ userId=" + userId);
        }
        return itemListResult;
    }
}
